package com.iemes.controller.inventory;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.entity.inventory.MxFormMap;
import com.iemes.entity.inventory.SummaryFormMap;
import com.iemes.mapper.inventory.SummaryMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/sc/")
public class SummaryController {
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Inject
    private SummaryMapper summaryMapper;

    @RequestMapping("scIndex")
    public String index(){
        return Common.BACKGROUND_PATH + "/inventory/summary/summaryIndex";
    }

    @ResponseBody
    @RequestMapping("getCount")
    public String getWorkOrderPage(){
        String month = "";
        SummaryFormMap[] list = new SummaryFormMap[12];
        List<SummaryFormMap> summaryFormMaps = summaryMapper.getCount();
        for (int i = 0; i < 12; i++){
            SummaryFormMap s = new SummaryFormMap();
            s.set("quantityNo",0);
            s.set("actualNo",0);
            s.set("WSingleTime",summaryFormMaps.get(0).get("WSingleTime").toString());
            list[i] = s;
        }
        for (SummaryFormMap sf : summaryFormMaps){
            sf.set("WSingleTime",sdf.format(sf.get("WSingleTime")));
            sf.set("quantityNo",sf.get("quantity"));
            //判断月份，根据月份进行存储
            month = sf.get("WSingleTime").toString().substring(5,7);
            list[Integer.parseInt(month) - 1] = sf;
        }
        return JSONObject.toJSONString(list);
    }

    @RequestMapping("scMaterialIndex")
    public String materialIndex(){
        return Common.BACKGROUND_PATH + "/inventory/summary/materialIndex";
    }


    @ResponseBody
    @RequestMapping("getMaterialCount")
    public String getMaterialCount(){
        //先获取
        String month = "";
        SummaryFormMap[] list = new SummaryFormMap[12];
        List<SummaryFormMap> summaryFormMaps = summaryMapper.getCount();
        for (int i = 0; i < 12; i++){
            SummaryFormMap s = new SummaryFormMap();
            s.set("quantityNo",0);
            s.set("actualNo",0);
            s.set("WSingleTime",summaryFormMaps.get(0).get("WSingleTime").toString());
            list[i] = s;
        }
        for (SummaryFormMap sf : summaryFormMaps){
            sf.set("WSingleTime",sdf.format(sf.get("WSingleTime")));
            sf.set("quantityNo",sf.get("quantity"));
            //判断月份，根据月份进行存储
            month = sf.get("WSingleTime").toString().substring(5,7);
            list[Integer.parseInt(month) - 1] = sf;
        }
        return JSONObject.toJSONString(list);
    }

}
